//
// Created by liuhuan on 2021/5/22.
//

#include <iostream>
#include <gtest/gtest.h>
#include "ValueRange.h"
#include "math.h"

using namespace std;

ValueRange::ValueRange() {
    this->start = 0;
    this->end = 0;
}

ValueRange::ValueRange(uint start, uint end) {
    if (start > end) {
        throw range_error("Invalid range: the start of the range is greater than the end");
    }

    this->start = start;
    this->end = end;
}

ValueRange::ValueRange(ValueRange const &other) {
    start = other.start;
    end = other.end;
}

uint ValueRange::size() {
    return end - start + 1;
}

bool ValueRange::contains(uint number) {
    return number >= start && number <= end;
}

uint ValueRange::rangeBitSize() {
    return uint(ceil(log2(this->size())));
}


TEST(testValueRange, testRange) {
    ValueRange range = ValueRange();
    EXPECT_LE(range.start, range.end);
}
TEST(testValueRange, testRangeSimple) {
    ValueRange range = ValueRange(2, 1000);
    EXPECT_LE(range.start, range.end);
    EXPECT_EQ(range.size(), 999);
    for (int i=range.start; i<=range.end; i++) {
        ASSERT_TRUE(range.contains(i));
    }
    ASSERT_FALSE(range.contains(range.start - 1));
    ASSERT_FALSE(range.contains(range.end + 1));
    EXPECT_EQ(range.rangeBitSize(), 10);
}
